package com.taojin.shoppinginterest.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.adapter.ListDetailFragmentAdapter;
import com.taojin.shoppinginterest.bean.ListDetail;
import com.taojin.shoppinginterest.bean.ListDetailInfo;
import com.taojin.shoppinginterest.constants.ApiConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by T400 on 15-5-14.
 */
@ContentView(R.layout.activity_listfragment_detail)
public class ListActivity extends ActionBarActivity {

    @ViewInject(R.id.activity_listFragment_detail_image)
    private ImageView imageView;
    @ViewInject(R.id.activity_listFragment_detail_advise)
    private TextView textView;
    @ViewInject(R.id.activity_listFragment_detail_listview)
    private ListView listView;
    private ListDetailFragmentAdapter listDetailFragmentAdapter;
    private List<ListDetail> listDetail;

    private List<ListDetailInfo> listInfo;
    private HttpUtils httpUtils;
    private BitmapUtils bitmapUtils;
    private View headerView;
    private String ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        ID = intent.getStringExtra("ID");

        initView();
        loadData();
    }

    public void initView() {
        listDetail = new ArrayList<>();
        listInfo = new ArrayList<>();
        bitmapUtils = new BitmapUtils(getApplication());
        httpUtils = new HttpUtils();
        listDetailFragmentAdapter = new ListDetailFragmentAdapter(listInfo, this);
        listView.setAdapter(listDetailFragmentAdapter);
    }

    private View getHeaderView(LayoutInflater inflater) {
        headerView = inflater.inflate(R.layout.headview_listfragment, null);
        return headerView;
    }

    public void loadData() {
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("app", "other");
        requestParams.addBodyParameter("act", "top_info");
        requestParams.addBodyParameter("ver", "1.1");
        requestParams.addBodyParameter("lat", "31.269738");
        requestParams.addBodyParameter("lon", "121.516908");
        requestParams.addBodyParameter("page", "1");
        requestParams.addBodyParameter("perpage", "20");
        requestParams.addBodyParameter("id", String.valueOf(ID));
        requestParams.addBodyParameter("pt", "2");

        httpUtils.send(HttpRequest.HttpMethod.POST, ApiConfig.URL, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String data = responseInfo.result;
                ListDetail listDetail = JSON.parseObject(data, ListDetail.class);
                if (listDetail != null) {
                    listInfo.clear();
                    listInfo.addAll(listDetail.getData().getList());
                    listDetailFragmentAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }


}
